<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>私信管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/css/news.css"/>
    <style>
        li {
            list-style: inherit !important;
        }
    </style>
</head>

<body class="childrenBody">
<form class="layui-form" action="">

    <div class="layui-form-item">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" lay-verify="required" placeholder="请输入标题" class="layui-input"
                   id="title">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <input type="text" lay-verify="required" placeholder="请输入内容" class="layui-input"
                   id="content">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">发送给用户</label>
            <div class="layui-input-inline">
                <select id="drpGoods" lay-search="required" lay-filter="drpGoods">
                    <option value="">群发</option>
                </select>
            </div>
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <a class="layui-btn layui-btn-normal" lay-submit="" lay-filter="add" id="add">立即发送</a>
            <a class="layui-btn layui-btn-primary close">关闭</a>
        </div>
    </div>
</form>
<script type="text/javascript" src="../../js/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript" src="../../js/wangEditor.js"></script>
<script type="text/javascript" src="../../js/basic.js"></script>
<script>


    function searchGoods() {

        $.ajax({
            type: "post",
            url: "/user/list",
            data: {
            },
            dataType: "json",
            success: function (data) {
                if (data.status == 0) {
                    resList=data.result;
                    for (var i = 0; i < data.result.length; i++) {
                        var op = document.createElement("option");
                        op.text = data.result[i].nickname ;
                        op.value =data.result[i].id+"";
                        document.getElementById("drpGoods").appendChild(op);
                        console.log(op)
                    }
                    layui.form.render();

                } else {
                    layer.msg(data.msg, {icon: 1});
                }
            }
        });

    }


    function add() {
        var topM = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        $.ajax({
            type: "post",
            url: "/usermessage/send",
            data: {
                content: $("#content").val(),
                userId: $("#drpGoods").val(),
                title: $("#title").val()
            },
            dataType: "json",
            success:

                function (data) {
                    backData(data, topM);
                }
        })
    };

    function backData(data, topM) {
        if (data.status == 0) {

            setTimeout(function () {
                layer.close(topM);
                top.layer.msg("发送成功！");
                //刷新父页面
                parent.getList({});
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            }, 500);
        } else {
            layer.close(topM);
            layer.msg(data.msg, {icon: 1});
        }
    }


    layui.use(['form'], function () {
        var form = layui.form;
        //监听提交
        form.on('submit(add)', function (data) {
            add();
            return false;
        });
        searchGoods();
    });

</script>
</body>
</html>